

*****************************************************
*****************************************************
***** generate mover tables for online appendix *****
*****************************************************
cap mkdir `"$parent/paper/online_appendix"'

quietly {
	
//use average commercial fees 
use `"$indices/fee_indices.dta"', clear 
keep if pay_type_pooled == 3 
keep payee_state_cd mean_ln_line_item_value
rename mean_ln_line_item_value avg_commercial_fee
tempfile comm_fee
save `comm_fee'


use `"$mover_data/$mover_file_size"', clear

merge m:1 payee_state_cd using `"$indices/fee_indices_medicaid.dta"', keepusing(pi*)
keep if _merge == 3
drop _merge 

merge m:1 payee_state_cd using `"$indices/cip_indices_medicaid.dta"', keepusing(*CIP*)
keep if _merge == 3
drop _merge 

merge m:1 payee_state_cd using `comm_fee'
keep if _merge == 3
drop _merge 

//merge controls 
merge m:1 year FIPS using `"$control_var/county_controls.dta"'
keep if _merge == 3 // all data from master file merge 
drop _merge 

destring statefips, replace  
merge m:1 year statefips using `"$control_var/control_vars_state.dta"'
keep if _merge == 3 //all data from the master file merge 
drop _merge 

*******Generate Pre-Post Differences*****
sort npimedicare_id YearPost
unab controllist: mean_ma_pen mean_number_mediciad_elig mean_dual mean_unins65 mean_number_medicare_elig mean_medicare_enrol avg_commercial_fee
unab indexlist: pi_c_FE_cpt_line_pcp pi_FE_cpt_line pi_c_FE_cpt_line pi_c_W_FE_cpt_line  ???_CIP_FE_fv ???_CIP_FE_fve ???_CIP_FE_nofe ???_CIP_FE_preg ???_CIP_FE ???_CIP_FE_nofe_fv ???_CIP_FE_nofe_fve ???_CIP_FE_W_nofe ???_CIP_FE_nofe_preg ???_CIP_FE_W ???_CIP_FE_nofe_icd9 ???_CIP_FE_icd9 ???_CIP_FE_nofe_inc ???_CIP_FE_inc ???_CIP_FE_nofe_rf ???_CIP_FE_rf CIP_abs CIP_abs_nofe pi_c_FE_cpt_line_abs

unab denial_indexlist: v_initial_den_CIP 

unab year_controllist: hinsva hinscare hinscaid hcovany veteran unempl med_hh_ince avg_hhc county_pop tot_active_md poverty white

foreach var of varlist `controllist' `indexlist' `indexlist_rf' `denial_indexlist' {
	
	keep if `var' != . 
	
	tempvar feevalpost feevalpre feevalpost2 feevalpre2
	
	by npimedicare_id YearPost: gegen `feevalpost' = mean(`var') if event_post
	by npimedicare_id YearPost: gegen `feevalpre' = mean(`var') if !event_post
	
	by npimedicare_id: gegen `feevalpost2' = mean(`feevalpost') 
	by npimedicare_id: gegen `feevalpre2' = mean(`feevalpre') 
	
	gen diffm`var' = `feevalpost2' - `feevalpre2' 
	
	drop `feevalpost' `feevalpre' `feevalpost2' `feevalpre2'	
}

cap drop MDsPerCapita
gen MDsPerCapita = tot_active_md/county_pop

foreach c of varlist veteran unempl poverty white {
	
	replace `c' = `c'/county_pop
}

foreach var of varlist `year_controllist' MDsPerCapita {
	
	keep if `var' != . 
	
}




local health_ins_market "c.diffmmean_ma_pen c.diffmmean_number_mediciad_elig c.diffmmean_dual c.diffmmean_unins65 c.diffmmean_number_medicare_elig c.diffmmean_medicare_enrol" 
local health_ins_market_loop "diffmmean_ma_pen diffmmean_number_mediciad_elig diffmmean_dual diffmmean_unins65 diffmmean_number_medicare_elig diffmmean_medicare_enrol" 

foreach var of local health_ins_market_loop {
	
	gen `var'_p = `var'*event_post 
	
}

gen diffmavg_commercial_fee_p = diffmavg_commercial_fee*event_post

unab health_ins_market_post: diffmmean_ma_pen_p diffmmean_number_mediciad_elig_p diffmmean_dual_p diffmmean_unins65_p diffmmean_number_medicare_elig_p diffmmean_medicare_enrol_p

unab health_ins_market_year: hinscaid hcovany hinscare avg_hhc 
unab health_prov_year: tot_active_md MDsPerCapita
unab ses_year: unempl white county_pop veteran poverty med_hh_ince


xtset npimedicare_id

//generate locals for fee and CIP variables to be used in the mover regressions
local loss_denial v_initial_den_CIP
local loss lev_CIP_FE
local loss_heck lev_CIP_FE_nofe
local loss_dollar CIP_abs 
local loss_dollar_heck CIP_abs_nofe	
local fee pi_c_FE_cpt_line
local fee_level pi_c_FE_cpt_line_abs

local lossw lev_CIP_FE_W
local lossw_heck lev_CIP_FE_W_nofe
local feew pi_c_W_FE_cpt_line


//generate post-move interactions
gen diffm`loss_denial'_post = diffm`loss_denial'*event_post
gen diffm`loss'_post = diffm`loss'*event_post
gen diffm`loss_heck'_post = diffm`loss_heck'*event_post
gen diffm`loss_dollar'_post = diffm`loss_dollar'*event_post
gen diffm`loss_dollar_heck'_post = diffm`loss_dollar_heck'*event_post

gen diffm`fee'_post = diffm`fee'*event_post
gen diffm`fee_level'_post = diffm`fee_level'*event_post

gen diffm`lossw'_post = diffm`lossw'*event_post
gen diffm`lossw_heck'_post = diffm`lossw_heck'*event_post
gen diffm`feew'_post = diffm`feew'*event_post


//generate locals for renaming the variables in the output table 
local loss_name 1.event_post#c.diffm`loss' diffmCIP diffm`loss'_post diffmCIP ///
	1.event_post#c.diffm`loss_heck' diffmCIP diffm`loss_heck'_post diffmCIP 
local fee_name 1.event_post#c.diffm`fee' diffmFee diffm`fee'_post diffmFee	

local lossw_name 1.event_post#c.diffm`lossw' diffmCIP diffm`lossw'_post diffmCIP ///
	1.event_post#c.diffm`lossw_heck' diffmCIP diffm`lossw_heck'_post diffmCIP 
local feew_name 1.event_post#c.diffm`feew' diffmFee diffm`feew'_post diffmFee	

local comm_fee_name 1.event_post#c.diffmavg_commercial_fee comm_fee ///
	diffmavg_commercial_fee_p comm_fee

local loss_level_name 1.event_post#c.diffm`loss_dollar' diffmCIP ///
	1.event_post#c.diffm`loss_dollar_heck' diffmCIP diffm`loss_dollar'_post ///
	diffmCIP diffm`loss_dollar_heck'_post diffmCIP
	
local fee_level_name 1.event_post#c.diffm`fee_level' diffmFee ///
	diffm`fee_level'_post diffmFee	

}


*********************************************************
******************** SAMPLE SELECTION AND COUNT NUMBER OF PHYSICIANS
*************************************************************
gen accept_medicare = 1 if medicare == "Y"
replace accept_medicare = 0 if medicare == "N"
replace accept_medicare = . if acceptinsurance == . 
bys npimedicare_id: gegen mean_medicare = mean(accept_medicare)
gen capacity = 1 - mean_medicare
** select only physicians accepting Medicare
****Select Sample 
local sampleselect   "(nummoves==1 | nummoves==0) & ( eventtime >=-3 & eventtime <=4 | eventtime == . ) & capacity == 0 " 
local sampleselect_full  "(nummoves==1 | nummoves==0) & ( eventtime >=-3 & eventtime <=4 | eventtime == . ) " 

****
preserve 
keep if `sampleselect'
drop if acceptinsurance == . 
*Determine # of Physicians 
gen physind = 0
bys npimedicare_id: replace physind = 1 if _n == 1 
gegen phys_count = sum(physind)
local physcount : di %7.0fc phys_count
restore 

preserve 
keep if `sampleselect_full'
drop if acceptinsurance == . 
*Determine # of Physicians 
gen physind = 0
bys npimedicare_id: replace physind = 1 if _n == 1 
gegen phys_count = sum(physind)
local physcount_full : di %7.0fc phys_count
restore 
****	
	
	
	
	
************************************************************************
************************************************************************
******* Mover Regression using the full sample -- Table S.11 ***********
************************************************************************
************************************************************************
	
************************************************************************	
****************** COLUMN 1 ** No selection, Restricted sample OLS
areg acceptinsurance  (c.diffm`fee' c.diffm`loss' `health_ins_market')##(i.event_post) `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect_full', absorb(npimedicare_id) vce(cluster paystateid)
		
	estadd local pys_number "`physcount_full'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "OLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "No"
	eststo mainmovers1
	
************************************************************************
************************************************************************	
****************** COLUMN 2 ** No selection, Unrestricted sample IV
ivreghdfe acceptinsurance  diffm`fee'_post event_post (diffm`loss'_post = diffm`loss_denial'_post) `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect_full', absorb(npimedicare_id) cluster(paystateid) first
	
	estadd local pys_number "`physcount_full'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "2SLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "No"
	eststo mainmovers2


************************************************************************	
************************************************************************
****************** COLUMN 3 ** Selection Correction, Unrestricted sample OLS
areg acceptinsurance  diffm`fee'_post event_post diffm`loss_heck'_post   `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect_full', absorb(npimedicare_id) vce(cluster paystateid)

	estadd local pys_number "`physcount_full'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "OLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "No"
	eststo mainmovers3

************************************************************************	
************************************************************************
****************** COLUMN 4 ** Selection Correction, Unrestricted sample IV
	ivreghdfe acceptinsurance  diffm`fee'_post event_post (diffm`loss_heck'_post  = diffm`loss_denial'_post ) `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect_full', absorb(npimedicare_id) cluster(paystateid) first
	
	estadd local pys_number "`physcount_full'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "2SLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "No"
	eststo mainmovers4



************************************************************************	
************************************************************************	
************************************************************************	
************************************************************************	
*************** GENERATE .TEX TABLE
	
	#delimit;
		esttab mainmovers1 mainmovers2 mainmovers3 mainmovers4 using "$online_appendix/online_appendix_mover_unrestricted.tex" ,  replace substitute(_ \_)
		nomtitles booktabs
		mgroups("Accept Medicaid Patients?", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
		rename(`loss_name' `fee_name')
		keep(diffmCIP diffmFee) 
		order(diffmCIP diffmFee)
		coeflabels(diffmFee "Post-move $\times \Delta \log \pi$ index"
				   diffmCIP "Post-move $\times \Delta \tau$ index"

		)
		se b(4) se(4 ) stats(instrument Sample pys_number N Phys_FE Controls phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Estimator}" "\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Physician FE}" "\footnotesize{Controls}"  "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
		starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;
		#delimit cr
		
		
		
		
		
************************************************************************
************************************************************************
* Mover Regression including Commercial Fees as controls -- Table S.16 *
************************************************************************
************************************************************************
eststo clear 
************************************************************************	
****************** COLUMN 1 ** No selection, Restricted sample OLS
areg acceptinsurance  (c.diffm`fee' c.diffm`loss' `health_ins_market' c.diffmavg_commercial_fee)##(i.event_post) `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) vce(cluster paystateid)
		
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "OLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	estadd local comm_contr "Yes"
	eststo mainmovers1
	
************************************************************************
************************************************************************	
****************** COLUMN 2 ** No selection, Restricted sample IV
ivreghdfe acceptinsurance  diffm`fee'_post event_post (diffm`loss'_post = diffm`loss_denial'_post) diffmavg_commercial_fee_p `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) cluster(paystateid) first
	
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "2SLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	estadd local comm_contr "Yes"
	eststo mainmovers2


************************************************************************	
************************************************************************
****************** COLUMN 3 ** Selection Correction, Restricted sample OLS
areg acceptinsurance  (c.diffm`fee' c.diffm`loss_heck' `health_ins_market' c.diffmavg_commercial_fee)##(i.event_post) `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) vce(cluster paystateid)

	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "OLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	estadd local comm_contr "Yes"
	eststo mainmovers3

************************************************************************	
************************************************************************
****************** COLUMN 4 ** Selection Correction, Restricted sample IV
ivreghdfe acceptinsurance  diffm`fee'_post event_post (diffm`loss_heck'_post  = diffm`loss_denial'_post ) diffmavg_commercial_fee_p `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) cluster(paystateid) first
	
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "2SLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	estadd local comm_contr "Yes"
	eststo mainmovers4



************************************************************************	
************************************************************************	
************************************************************************	
************************************************************************	
*************** GENERATE .TEX TABLE
	
	#delimit;
		esttab mainmovers1 mainmovers2 mainmovers3 mainmovers4 using "$online_appendix/online_appendix_mover_commfee_contrl.tex" ,  replace substitute(_ \_)
		nomtitles booktabs
		mgroups("Accept Medicaid Patients?", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
		rename(`loss_name' `fee_name' `comm_fee_name')
		keep(diffmCIP diffmFee comm_fee) 
		order(diffmCIP diffmFee comm_fee)
		coeflabels(diffmFee "Post-move $\times \Delta \log \pi$ index"
				   diffmCIP "Post-move $\times \Delta \tau$ index"
				   comm_fee "Post-move $\times \Delta$ mean log commercial fee"

		)
		se b(4) se(4 ) stats(instrument Sample pys_number N Phys_FE Controls comm_contr phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Estimator}" "\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Physician FE}" "\footnotesize{Controls}" "\footnotesize{Controlling for Commercial Fees}"  "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
		starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;
		#delimit cr
		

	

************************************************************************
************************************************************************
***** Mover Regression using indices weighted by RVUs (Table S.9) ******
************************************************************************
************************************************************************
eststo clear 
************************************************************************	
****************** COLUMN 1 ** No selection, Restricted sample OLS
areg acceptinsurance  (c.diffm`feew' c.diffm`lossw' `health_ins_market')##(i.event_post) `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) vce(cluster paystateid)
		
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "OLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers1
	
************************************************************************
************************************************************************	
****************** COLUMN 2 ** No selection, Restricted sample IV
ivreghdfe acceptinsurance  diffm`feew'_post event_post (diffm`lossw'_post = diffm`loss_denial'_post) `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) cluster(paystateid) first
	
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "2SLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers2


************************************************************************	
************************************************************************
****************** COLUMN 3 ** Selection Correction, Restricted sample OLS
areg acceptinsurance  (c.diffm`feew' c.diffm`lossw_heck' `health_ins_market')##(i.event_post) `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) vce(cluster paystateid)

	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "OLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers3

************************************************************************	
************************************************************************
****************** COLUMN 4 ** Selection Correction, Restricted sample IV
ivreghdfe acceptinsurance  diffm`feew'_post event_post (diffm`lossw_heck'_post  = diffm`loss_denial'_post ) `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) cluster(paystateid) first
	
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "2SLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers4



************************************************************************	
************************************************************************	
************************************************************************	
************************************************************************	
*************** GENERATE .TEX TABLE
	
	#delimit;
		esttab mainmovers1 mainmovers2 mainmovers3 mainmovers4 using "$online_appendix/online_appendix_mover_weighted.tex" ,  replace substitute(_ \_)
		nomtitles booktabs
		mgroups("Accept Medicaid Patients?", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
		rename(`lossw_name' `feew_name')
		keep(diffmCIP diffmFee) 
		order(diffmCIP diffmFee)
		coeflabels(diffmFee "Post-move $\times \Delta \log \pi$ index"
				   diffmCIP "Post-move $\times \Delta \tau$ index"

		)
		se b(4) se(4 ) stats(instrument Sample pys_number N Phys_FE Controls phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Estimator}" "\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Physician FE}" "\footnotesize{Controls}"  "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
		starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;
		#delimit cr
		

	
	
***********************************************************************
***********************************************************************
**** Appendix Table 2 -- PCP specific Fee Index (Table S.5) ***********
***********************************************************************
***********************************************************************

foreach adj_fee of varlist pi_c_FE_cpt_line_pcp pi_FE_cpt_line {
	
	local adj_fee_name `"`adj_fee' Fee"'
	if "`adj_fee'" == "pi_c_FE_cpt_line_pcp" {
		local x "pcp_fee"
	}
	
	gen diffm`adj_fee'_post = diffm`adj_fee'*event_post
	local feeadj_name 1.event_post#c.diffm`adj_fee' diffmFee diffm`adj_fee'_post diffmFee	
	
	
	eststo clear 
************************************************************************	
****************** COLUMN 1 ** No selection, Restricted sample OLS
areg acceptinsurance  (c.diffm`adj_fee' c.diffm`loss' `health_ins_market')##(i.event_post) `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) vce(cluster paystateid)
		
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "OLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers1
	
************************************************************************
************************************************************************	
****************** COLUMN 2 ** No selection, Restricted sample IV
ivreghdfe acceptinsurance  diffm`adj_fee'_post event_post (diffm`loss'_post = diffm`loss_denial'_post) `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) cluster(paystateid) first
	
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "2SLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers2


************************************************************************	
************************************************************************
****************** COLUMN 3 ** Selection Correction, Restricted sample OLS
areg acceptinsurance  (c.diffm`adj_fee' c.diffm`loss_heck' `health_ins_market')##(i.event_post) `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) vce(cluster paystateid)

	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "OLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers3

************************************************************************	
************************************************************************
****************** COLUMN 4 ** Selection Correction, Restricted sample IV
ivreghdfe acceptinsurance  diffm`adj_fee'_post event_post (diffm`loss_heck'_post  = diffm`loss_denial'_post ) `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) cluster(paystateid) first
	
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "2SLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers4



************************************************************************	
************************************************************************	
************************************************************************	
************************************************************************	
*************** GENERATE .TEX TABLE
	
	#delimit;
		esttab mainmovers1 mainmovers2 mainmovers3 mainmovers4 using "$online_appendix/online_appendix_mover_`x'.tex" ,  replace substitute(_ \_)
		nomtitles booktabs
		mgroups("Accept Medicaid Patients?", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
		rename(`loss_name' `feeadj_name')
		keep(diffmCIP diffmFee) 
		order(diffmCIP diffmFee)
		coeflabels(diffmFee "Post-move $\times \Delta \log \pi$ index"
				   diffmCIP "Post-move $\times \Delta \tau$ index"

		)
		se b(4) se(4 ) stats(instrument Sample pys_number N Phys_FE Controls phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Estimator}" "\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Physician FE}" "\footnotesize{Controls}"  "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
		starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;
		#delimit cr
		

	
}





*********************************************************************
*********************************************************************
****** Mover Regressions with adjusted CIP indices (Table S.7) ******
*********************************************************************
*********************************************************************
local adjusted_cip_indices `"_rf"'

foreach adj_loss of local adjusted_cip_indices {
	
	
	gen diffmlev_CIP_FE`adj_loss'_post = diffmlev_CIP_FE`adj_loss'*event_post
	gen diffmlev_CIP_FE_nofe`adj_loss'_post = diffmlev_CIP_FE_nofe`adj_loss'*event_post

	local lossadj_name `""1.event_post#c.diffmlev_CIP_FE_nofe`adj_loss'" " diffmCIP " "diffmlev_CIP_FE`adj_loss'_post" " diffmCIP" "1.event_post#c.diffmlev_CIP_FE`adj_loss'" " diffmCIP" "diffmlev_CIP_FE_nofe`adj_loss'_post" " diffmCIP""'
	di `lossadj_name'




eststo clear 
************************************************************************	
****************** COLUMN 1 ** No selection, No MCO, Restricted sample OLS, No Size
areg acceptinsurance  (c.diffm`fee' c.diffmlev_CIP_FE`adj_loss' `health_ins_market')##(i.event_post) `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) vce(cluster paystateid)
		
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "OLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers1
	
************************************************************************
************************************************************************	
****************** COLUMN 2 ** No selection, No MCO, Restricted sample IV, No Size
ivreghdfe acceptinsurance  diffm`fee'_post event_post (diffmlev_CIP_FE`adj_loss'_post = diffm`loss_denial'_post) `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) cluster(paystateid) first
	
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "2SLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers2


************************************************************************	
************************************************************************
****************** COLUMN 3 ** Selection Correction, No MCO, Restricted sample OLS, No Size
areg acceptinsurance  (c.diffm`fee' c.diffmlev_CIP_FE_nofe`adj_loss' `health_ins_market')##(i.event_post) `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) vce(cluster paystateid)

	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "OLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers3

************************************************************************	
************************************************************************
****************** COLUMN 4 ** Selection Correction, No MCO, Restricted sample IV, No Size
ivreghdfe acceptinsurance  diffm`fee'_post event_post (diffmlev_CIP_FE_nofe`adj_loss'_post  = diffm`loss_denial'_post) `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) cluster(paystateid) first
	
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "2SLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers4



************************************************************************	
************************************************************************	
************************************************************************	
************************************************************************	
*************** GENERATE .TEX TABLE
	
	#delimit;
		esttab mainmovers1 mainmovers2 mainmovers3 mainmovers4 using "$online_appendix/online_appendix_mover`adj_loss'.tex" ,  replace substitute(_ \_)
		nomtitles booktabs
		mgroups("Accept Medicaid Patients?", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
		rename(`lossadj_name' `fee_name')
		keep(diffmCIP diffmFee) 
		order(diffmCIP diffmFee)
		coeflabels(diffmFee "Post-move $\times \Delta \log \pi$ index"
				   diffmCIP "Post-move $\times \Delta \tau$ index"

		)
		se b(4) se(4 ) stats(instrument Sample pys_number N Phys_FE Controls phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Estimator}" "\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Physician FE}" "\footnotesize{Controls}"  "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
		starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;
		#delimit cr
		

	
}



*********************************************************************
*********************************************************************
************ Mover Regressions using CIP in Dollar (Table S.18) *****
*********************************************************************
*********************************************************************
eststo clear 
************************************************************************	
****************** COLUMN 1 ** No selection, No MCO, Restricted sample OLS, No Size
areg acceptinsurance  (c.diffm`fee' c.diffm`loss_dollar' `health_ins_market')##(i.event_post) `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) vce(cluster paystateid)
		
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "OLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers1
	

************************************************************************	
****************** COLUMN 2 ** No selection, No MCO, Restricted sample IV, No Size
ivreghdfe acceptinsurance  diffm`fee'_post event_post (c.diffm`loss_dollar'_post = diffm`loss_denial'_post) `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) cluster(paystateid) first
	
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "2SLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers2


	
************************************************************************
****************** COLUMN 3 ** Selection Correction, No MCO, Restricted sample OLS, No Size
areg acceptinsurance  (c.diffm`fee' c.diffm`loss_dollar_heck' `health_ins_market')##(i.event_post) `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) vce(cluster paystateid)

	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "OLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers3

	
************************************************************************
****************** COLUMN 4 ** Selection Correction, No MCO, Restricted sample IV, No Size
ivreghdfe acceptinsurance  diffm`fee'_post event_post (c.diffm`loss_dollar_heck'_post  = diffm`loss_denial'_post) `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) cluster(paystateid) first
	
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "2SLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers4


	
************************************************************************	
*************** GENERATE .TEX TABLE
	
	#delimit;
		esttab mainmovers1 mainmovers2 mainmovers3 mainmovers4 using "$online_appendix/online_appendix_mover_CIP_dollar.tex" ,  replace substitute(_ \_)
		nomtitles booktabs
		mgroups("Accept Medicaid Patients?", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
		rename(`loss_level_name' `fee_name')
		keep(diffmCIP diffmFee) 
		order(diffmCIP diffmFee)
		coeflabels(diffmFee "Post-move $\times \Delta \log \pi$ index"
				   diffmCIP "Post-move $\times \Delta$ CIP index"

		)
		se b(4) se(4 ) stats(instrument Sample pys_number N Phys_FE Controls phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Estimator}" "\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Physician FE}" "\footnotesize{Controls}"  "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
		starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;
		#delimit cr


		

**************************************************************************
**************************************************************************
***** Mover Regressions using CIP in Dollar and Level Fee (Table S.20) ***
**************************************************************************
**************************************************************************
eststo clear 
************************************************************************	
****************** COLUMN 1 ** No selection, No MCO, Restricted sample OLS, No Size
areg acceptinsurance  (c.diffm`fee_level' c.diffm`loss_dollar' `health_ins_market')##(i.event_post) `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) vce(cluster paystateid)
		
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "OLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers1
	

************************************************************************	
****************** COLUMN 2 ** No selection, No MCO, Restricted sample IV, No Size
ivreghdfe acceptinsurance  diffm`fee_level'_post event_post (c.diffm`loss_dollar'_post = diffm`loss_denial'_post) `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) cluster(paystateid) first
	
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "2SLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers2


	
************************************************************************
****************** COLUMN 3 ** Selection Correction, No MCO, Restricted sample OLS, No Size
areg acceptinsurance  (c.diffm`fee_level' c.diffm`loss_dollar_heck' `health_ins_market')##(i.event_post) `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) vce(cluster paystateid)

	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "OLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers3

	
************************************************************************
****************** COLUMN 4 ** Selection Correction, No MCO, Restricted sample IV, No Size
ivreghdfe acceptinsurance  diffm`fee_level'_post event_post (c.diffm`loss_dollar_heck'_post  = diffm`loss_denial'_post) `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) cluster(paystateid) first
	
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "2SLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	eststo mainmovers4


	
************************************************************************	
*************** GENERATE .TEX TABLE
	
	#delimit;
		esttab mainmovers1 mainmovers2 mainmovers3 mainmovers4 using "$online_appendix/online_appendix_mover_fee_level_CIP_dollar.tex" ,  
		replace substitute(_ \_) nomtitles booktabs
		mgroups("Accept Medicaid Patients?", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
		rename(`loss_level_name' `fee_level_name')
		keep(diffmCIP diffmFee) 
		order(diffmCIP diffmFee)
		coeflabels(diffmFee "Post-move $\times \Delta \pi$ index"
				   diffmCIP "Post-move $\times \Delta$ CIP index"

		)
		se b(4) se(4 ) stats(instrument Sample pys_number N Phys_FE Controls phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Estimator}" "\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Physician FE}" "\footnotesize{Controls}"  "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
		starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;
		#delimit cr

	
		
		
	
		
********************************************************************************
********************************************************************************
******* Mover Regression using the MCO/FFS specific Index (Table S.22) *********
********************************************************************************
********************************************************************************

quietly {
	
//use the mco enrolle/medicaid enrollee data to create mco shares 
use `"$parent/data/mco_data/medicaid_enrollment.dta"', clear 
merge 1:1 year payee_state_cd using `"$parent/data/mco_data/medicaid_mco_enrollment.dta"', nogen 

gen mco_share = mco_enroll/medicaid_enroll
replace mco_share = 1 if mco_share > 1 

keep payee_state_cd year  mco_share

tempfile mco_share_file
save `mco_share_file'

	
use `"$mover_data/$mover_file_size"', clear

merge m:1 payee_state_cd using `"$indices/fee_indices_medicaid.dta"', keepusing(pi*)
keep if _merge == 3
drop _merge 

merge m:1 payee_state_cd using `"$mcoindices/cip_indices_medicaid.dta"', keepusing(*CIP*)
keep if _merge == 3
drop _merge 

merge m:1 year payee_state_cd using `mco_share_file'
keep if _merge == 3
drop _merge 

//merge controls 
merge m:1 year FIPS using `"$control_var/county_controls.dta"'
keep if _merge == 3 // all data from master file merge 
drop _merge 

destring statefips, replace  
merge m:1 year statefips using `"$control_var/control_vars_state.dta"'
keep if _merge == 3 //all data from the master file merge 
drop _merge 

*******Generate Pre-Post Differences*****
sort npimedicare_id YearPost
unab controllist: mean_ma_pen mean_number_mediciad_elig mean_dual mean_unins65 mean_number_medicare_elig mean_medicare_enrol mco_share
unab indexlist: pi_c_FE_cpt_line_pcp pi_FE_cpt_line pi_c_FE_cpt_line lev_CIP_FE lev_CIP_FE_nofe

unab denial_indexlist: v_initial_den_CIP 

unab year_controllist: hinsva hinscare hinscaid hcovany veteran unempl med_hh_ince avg_hhc county_pop tot_active_md poverty white

foreach var of varlist `controllist' `indexlist' `indexlist_rf' `denial_indexlist' {
	
	keep if `var' != . 
	
	tempvar feevalpost feevalpre feevalpost2 feevalpre2
	
	by npimedicare_id YearPost: gegen `feevalpost' = mean(`var') if event_post
	by npimedicare_id YearPost: gegen `feevalpre' = mean(`var') if !event_post
	
	by npimedicare_id: gegen `feevalpost2' = mean(`feevalpost') 
	by npimedicare_id: gegen `feevalpre2' = mean(`feevalpre') 
	
	gen diffm`var' = `feevalpost2' - `feevalpre2' 
	
	drop `feevalpost' `feevalpre' `feevalpost2' `feevalpre2'	
}

cap drop MDsPerCapita
gen MDsPerCapita = tot_active_md/county_pop

foreach c of varlist veteran unempl poverty white {
	
	replace `c' = `c'/county_pop
}

foreach var of varlist `year_controllist' MDsPerCapita {
	
	keep if `var' != . 
	
}




local health_ins_market "c.diffmmean_ma_pen c.diffmmean_number_mediciad_elig c.diffmmean_dual c.diffmmean_unins65 c.diffmmean_number_medicare_elig c.diffmmean_medicare_enrol" 
local health_ins_market_loop "diffmmean_ma_pen diffmmean_number_mediciad_elig diffmmean_dual diffmmean_unins65 diffmmean_number_medicare_elig diffmmean_medicare_enrol" 

foreach var of local health_ins_market_loop {
	
	gen `var'_p = `var'*event_post 
	
}

gen diffmmco_share_p = diffmmco_share*event_post

unab health_ins_market_post: diffmmean_ma_pen_p diffmmean_number_mediciad_elig_p diffmmean_dual_p diffmmean_unins65_p diffmmean_number_medicare_elig_p diffmmean_medicare_enrol_p

unab health_ins_market_year: hinscaid hcovany hinscare avg_hhc 
unab health_prov_year: tot_active_md MDsPerCapita
unab ses_year: unempl white county_pop veteran poverty med_hh_ince


xtset npimedicare_id


local loss_denial v_initial_den_CIP
local loss lev_CIP_FE
local loss_heck lev_CIP_FE_nofe
local fee pi_c_FE_cpt_line

gen diffm`loss_denial'_post = diffm`loss_denial'*event_post
gen diffm`loss'_post = diffm`loss'*event_post
gen diffm`loss_heck'_post = diffm`loss_heck'*event_post
gen diffm`fee'_post = diffm`fee'*event_post



local loss_name 1.event_post#c.diffm`loss' diffmCIP diffm`loss'_post diffmCIP 1.event_post#c.diffm`loss_heck' diffmCIP diffm`loss_heck'_post diffmCIP 
local fee_name 1.event_post#c.diffm`fee' diffmFee diffm`fee'_post diffmFee	

}


*********************************************************
******************** SAMPLE SELECTION AND COUNT NUMBER OF PHYSICIANS
*************************************************************
gen accept_medicare = 1 if medicare == "Y"
replace accept_medicare = 0 if medicare == "N"
replace accept_medicare = . if acceptinsurance == . 
bys npimedicare_id: gegen mean_medicare = mean(accept_medicare)
gen capacity = 1 - mean_medicare
** select only physicians accepting Medicare
****Select Sample 
local sampleselect   "(nummoves==1 | nummoves==0) & ( eventtime >=-3 & eventtime <=4 | eventtime == . ) & capacity == 0 " 

****
preserve 
keep if `sampleselect'
drop if acceptinsurance == . 
*Determine # of Physicians 
gen physind = 0
bys npimedicare_id: replace physind = 1 if _n == 1 
gegen phys_count = sum(physind)
local physcount : di %7.0fc phys_count
restore 
****	
	
	
	
	
************************************************************************
************************************************************************
******* Mover Regression using the MCO/FFS specific Index **************
************************************************************************
************************************************************************
	
************************************************************************	
****************** COLUMN 1 ** No selection, Restricted sample OLS
areg acceptinsurance  (c.diffm`fee' c.diffm`loss' `health_ins_market' c.diffmmco_share)##(i.event_post) `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) vce(cluster paystateid)
		
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "OLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	estadd local mco_share "Yes"
	eststo mainmovers1
	
************************************************************************
************************************************************************	
****************** COLUMN 2 ** No selection, Restricted sample IV
ivreghdfe acceptinsurance  diffm`fee'_post event_post (diffm`loss'_post = diffm`loss_denial'_post) diffmmco_share_p `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) cluster(paystateid) first
	
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "2SLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	estadd local mco_share "Yes"
	eststo mainmovers2


************************************************************************	
************************************************************************
****************** COLUMN 3 ** Selection Correction, Restricted sample OLS
areg acceptinsurance  (c.diffm`fee' c.diffm`loss_heck' `health_ins_market' c.diffmmco_share)##(i.event_post) `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) vce(cluster paystateid)

	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "OLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	estadd local mco_share "Yes"
	eststo mainmovers3

************************************************************************	
************************************************************************
****************** COLUMN 4 ** Selection Correction, Restricted sample IV
	ivreghdfe acceptinsurance  diffm`fee'_post event_post (diffm`loss_heck'_post  = diffm`loss_denial'_post ) diffmmco_share_p `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) cluster(paystateid) first
	
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "2SLS"
	estadd local Phys_FE "Yes"
	estadd local Controls "Yes"
	estadd local Sample "Yes"
	estadd local mco_share "Yes"
	eststo mainmovers4



************************************************************************	
************************************************************************	
************************************************************************	
************************************************************************	
*************** GENERATE .TEX TABLE
	
	#delimit;
		esttab mainmovers1 mainmovers2 mainmovers3 mainmovers4 using "$online_appendix/online_appendix_mover_mcoffsindex.tex" ,  replace substitute(_ \_)
		nomtitles booktabs
		mgroups("Accept Medicaid Patients?", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
		rename(`loss_name' `fee_name')
		keep(diffmCIP diffmFee) 
		order(diffmCIP diffmFee)
		coeflabels(diffmFee "Post-move $\times \Delta \log \pi$ index"
				   diffmCIP "Post-move $\times \Delta \tau$ index"

		)
		se b(4) se(4 ) stats(instrument Sample pys_number N Phys_FE Controls mco_share phys_fe_index sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Estimator}" "\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Physician FE}" "\footnotesize{Controls}"  "\footnotesize{Control for State Level MCO Share}" "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
		starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;
		#delimit cr
		

		
		
		
		
		
		
		
		
		
		
		
		
*********************************************************************	
*********************************************************************
*********** Mover Regressions Interaction with  Groups Size**********
******************* Online Appendix Table S.13  *********************
*********************************************************************
*********************************************************************


use `"$mover_data/$mover_file_size"', clear

merge m:1 payee_state_cd using `"$indices/fee_indices_medicaid.dta"', keepusing(pi*)
keep if _merge == 3
drop _merge 

merge m:1 payee_state_cd using `"$indices/cip_indices_medicaid.dta"', keepusing(*CIP*)
keep if _merge == 3
drop _merge 

//merge controls 
merge m:1 year FIPS using `"$control_var/county_controls.dta"'
keep if _merge == 3 // all data from master file merge 
drop _merge 

destring statefips, replace  
merge m:1 year statefips using `"$control_var/control_vars_state.dta"'
keep if _merge == 3 //all data from the master file merge 
drop _merge 

*******Generate Pre-Post Differences*****
sort npimedicare_id YearPost
unab controllist: mean_ma_pen mean_number_mediciad_elig mean_dual mean_unins65 mean_number_medicare_elig mean_medicare_enrol 
unab indexlist: pi_c_FE_cpt_line ???_CIP_FE_nofe ???_CIP_FE 
unab denial_indexlist: v_initial_den_CIP  

unab year_controllist: hinsva hinscare hinscaid hcovany veteran unempl med_hh_ince avg_hhc county_pop tot_active_md poverty white

foreach var of varlist `controllist' `indexlist' `indexlist_rf' `denial_indexlist' {
	
	keep if `var' != . 
	
	tempvar feevalpost feevalpre feevalpost2 feevalpre2
	
	by npimedicare_id YearPost: gegen `feevalpost' = mean(`var') if event_post
	by npimedicare_id YearPost: gegen `feevalpre' = mean(`var') if !event_post
	
	by npimedicare_id: gegen `feevalpost2' = mean(`feevalpost') 
	by npimedicare_id: gegen `feevalpre2' = mean(`feevalpre') 
	
	gen diffm`var' = `feevalpost2' - `feevalpre2' 
	
	drop `feevalpost' `feevalpre' `feevalpost2' `feevalpre2'	
}

cap drop MDsPerCapita
gen MDsPerCapita = tot_active_md/county_pop

foreach c of varlist veteran unempl poverty white {
	
	replace `c' = `c'/county_pop
}

foreach var of varlist `year_controllist' MDsPerCapita {
	
	keep if `var' != . 
	
}




local health_ins_market "c.diffmmean_ma_pen c.diffmmean_number_mediciad_elig c.diffmmean_dual c.diffmmean_unins65 c.diffmmean_number_medicare_elig c.diffmmean_medicare_enrol" 
local health_ins_market_loop "diffmmean_ma_pen diffmmean_number_mediciad_elig diffmmean_dual diffmmean_unins65 diffmmean_number_medicare_elig diffmmean_medicare_enrol" 

foreach var of local health_ins_market_loop {
	
	gen `var'_p = `var'*event_post 
	
}

unab health_ins_market_post: diffmmean_ma_pen_p diffmmean_number_mediciad_elig_p diffmmean_dual_p diffmmean_unins65_p diffmmean_number_medicare_elig_p diffmmean_medicare_enrol_p

unab health_ins_market_year: hinscaid hcovany hinscare avg_hhc 
unab health_prov_year: tot_active_md MDsPerCapita
unab ses_year: unempl white county_pop veteran poverty med_hh_ince


xtset npimedicare_id





*********************************************************
******************** SAMPLE SELECTION AND COUNT NUMBER OF PHYSICIANS
*************************************************************
gen accept_medicare = 1 if medicare == "Y"
replace accept_medicare = 0 if medicare == "N"
replace accept_medicare = . if acceptinsurance == . 
bys npimedicare_id: gegen mean_medicare = mean(accept_medicare)
gen capacity = 1 - mean_medicare
** select only physicians accepting Medicare
****Select Sample 
local sampleselect   "(nummoves==1 | nummoves==0) & ( eventtime >=-3 & eventtime <=4 | eventtime == . ) & capacity == 0 " 
****
preserve 
keep if `sampleselect'
drop if acceptinsurance == . 
*Determine # of Physicians 
gen physind = 0
bys npimedicare_id: replace physind = 1 if _n == 1 
gegen phys_count = sum(physind)
local physcount : di %7.0fc phys_count
restore 
****	



gen coworker = phys_year - 1
//define locals and variables 
local loss_denial v_initial_den_CIP
local loss lev_CIP_FE
local loss_heck lev_CIP_FE_nofe
local fee pi_c_FE_cpt_line
gen diffm`loss_denial'_post = diffm`loss_denial'*event_post
gen diffm`loss'_post = diffm`loss'*event_post
gen diffm`loss_heck'_post = diffm`loss_heck'*event_post
gen diffm`fee'_post = diffm`fee'*event_post

//generate group size variables and locals

foreach size of varlist coworker {

	local sizename "Other Physicians in Group"
	local sizefile "mover_gsize-1"
		
	gen fee_`size' = diffm`fee'*`size'
	gen cip_`size' = diffm`loss'*`size'
	gen cipheck_`size' = diffm`loss_heck'*`size'
	gen denial_`size' = diffm`loss_denial'*`size'
	gen fee_`size'_post = diffm`fee'*`size'*event_post
	gen cip_`size'_post = diffm`loss'*`size'*event_post
	gen cipheck_`size'_post = diffm`loss_heck'*`size'*event_post
	gen denial_`size'_post = diffm`loss_denial'*`size'*event_post
	gen `size'_post = `size'*event_post


	//renaming
	local loss_name 1.event_post#c.diffm`loss' diffmCIP diffm`loss'_post diffmCIP 1.event_post#c.diffm`loss_heck' diffmCIP diffm`loss_heck'_post diffmCIP 
	local fee_name 1.event_post#c.diffm`fee' diffmFee diffm`fee'_post diffmFee	

	local loss_size_post cip_`size'_post sizeCIPpost cipheck_`size'_post sizeCIPpost
	local fee_size_post fee_`size'_post sizeFeepost 

	local loss_size cip_`size' sizeCIP cipheck_`size' sizeCIP 
	local fee_size fee_`size' sizeFee 


		
	**************************************************************************
	**************************************************************************
	***** Mover Regressions interacting coefficients with group size *********
	**************************************************************************
	**************************************************************************
		
	************************************************************************	
	****************** COLUMN 2 ** No selection, Restricted sample OLS
	areg acceptinsurance  fee_`size' fee_`size'_post cip_`size'_post c.`size' `size'_post cip_`size' (c.diffm`fee' c.diffm`loss' `health_ins_market')##(i.event_post) `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) vce(cluster paystateid)
			
		estadd local pys_number "`physcount'"
		estadd local sel_cor_index "No"
		estadd local phys_fe_index "Yes"
		estadd local instrument "OLS"
		estadd local Phys_FE "Yes"
		estadd local Controls "Yes"
		estadd local Sample "Yes"
		estadd local twowayint "Yes"
		eststo mainmovers1

	************************************************************************	
	************************************************************************
	****************** COLUMN 4 ** No selection, Restricted sample IV
		ivreghdfe acceptinsurance  diffm`fee'_post event_post fee_`size' fee_`size'_post c.`size' `size'_post (diffm`loss'_post cip_`size'_post cip_`size' = diffm`loss_denial'_post denial_`size'_post denial_`size') `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) cluster(paystateid) first
		
		estadd local pys_number "`physcount'"
		estadd local sel_cor_index "No"
		estadd local phys_fe_index "Yes"
		estadd local instrument "2SLS"
		estadd local Phys_FE "Yes"
		estadd local Controls "Yes"
		estadd local Sample "Yes"
		estadd local twowayint "Yes"
		eststo mainmovers2


	************************************************************************	
	************************************************************************
	****************** COLUMN 6 ** Selection Correction, Restricted sample OLS
	areg acceptinsurance  diffm`fee'_post event_post diffm`loss_heck'_post cipheck_`size'_post cipheck_`size' `size'_post fee_`size' fee_`size'_post c.`size' `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) vce(cluster paystateid)

		estadd local pys_number "`physcount'"
		estadd local sel_cor_index "Yes"
		estadd local phys_fe_index "No"
		estadd local instrument "OLS"
		estadd local Phys_FE "Yes"
		estadd local Controls "Yes"
		estadd local Sample "Yes"
		estadd local twowayint "Yes"
		eststo mainmovers3


	************************************************************************	
	************************************************************************
	****************** COLUMN 8 ** Selection Correction, Restricted sample IV
		ivreghdfe acceptinsurance  diffm`fee'_post event_post `size'_post (diffm`loss_heck'_post cipheck_`size'_post cipheck_`size' = diffm`loss_denial'_post denial_`size' denial_`size'_post) fee_`size' fee_`size'_post c.`size' `health_ins_market_post' `health_ins_market_year' `health_prov_year' `ses_year' ib2012.year if `sampleselect', absorb(npimedicare_id) cluster(paystateid) first
		
		estadd local pys_number "`physcount'"
		estadd local sel_cor_index "Yes"
		estadd local phys_fe_index "No"
		estadd local instrument "2SLS"
		estadd local Phys_FE "Yes"
		estadd local Controls "Yes"
		estadd local Sample "Yes"
		estadd local twowayint "Yes"
		eststo mainmovers4



	************************************************************************	
	************************************************************************	
	************************************************************************	
	************************************************************************	
	*************** GENERATE .TEX TABLE
		#delimit;

			esttab mainmovers1 mainmovers2 mainmovers3 mainmovers4 using `"$online_appendix/`sizefile'.tex"',  replace substitute(_ \_)
			nomtitles booktabs
			mgroups("Accept Medicaid Patients?", pattern(1 0 0 0 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
			rename(`loss_name' `fee_name' `loss_size_post' `fee_size_post')
			keep(diffmCIP diffmFee sizeCIPpost sizeFeepost `size') 
			order(diffmCIP sizeCIPpost diffmFee sizeFeepost `size')
			coeflabels(diffmFee "Post-move $\times \Delta \log \pi$ index"
					   diffmCIP "Post-move $\times \Delta \tau$ index"
					   sizeCIPpost "Post-move $\times \Delta \tau$ index $\times$ `sizename'"
					   sizeFeepost "Post-move $\times \Delta \log \pi$ index $\times$ `sizename'"
					   `size' "`sizename'"
			)
			se b(6) se(6) stats(instrument Sample pys_number N Phys_FE Controls twowayint phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Estimator}" "\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Physician FE}" "\footnotesize{Controls}" "\footnotesize{Two-Way Interactions}"  "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
			starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;
			
			
		#delimit cr
		
	
}
